package com.nice.nice201110.controller;

import com.nice.nice201110.entity.TBaseCapital;
import com.nice.nice201110.service.TbaseCapitalService;
import com.nice.nice201110.utils.NiceEnum;
import com.nice.nice201110.vo.UserVo;
import com.nice.nice201110.vo.BaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 *  资产基本类型 contorller
 *  1、POST /url 创建
 *  2、DELETE /url/xxx 删除
 *  3、PUT /url/xxx 更新
 *  4、GET /url/xxx 查看
 * */
@Controller
@RestController
@RequestMapping("/admin/tbaseCapital")
public class TbaseCapitalController {

    @Autowired
    TbaseCapitalService tbaseCapitalService;

    private Map<String,Object> codeMes = new HashMap<>();
    /**
     *  查询列表
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/list")
    public Map<String,Object> getlist(HttpServletRequest request,@RequestBody(required=false) BaseVo basevo) {
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        if(basevo!=null){
            basevo.setUservo(userVo);
        }else{
            basevo = new BaseVo();
            basevo.setUservo(userVo);
        }
        List<TBaseCapital> tBaseCapital = tbaseCapitalService.getlist(basevo);

        codeMes.put("code", NiceEnum.code_200);
        codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        codeMes.put("data", tBaseCapital);
        return codeMes;

    }
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/one")
    public Map<String,Object> getOne(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id) {
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        TBaseCapital tBaseCapital = tbaseCapitalService.getOne(userVo,id);
        if(tBaseCapital!=null){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tBaseCapital);
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     *  更新数据
     **/
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT,value="/update")
    public Map<String,Object> updateTbaseCapital(HttpServletRequest request,@RequestBody TBaseCapital tBaseCapital){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean =  tbaseCapitalService.updateTbaseCapital(userVo,tBaseCapital);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     *  插入数据
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/insert")
    public Map<String,Object> insetTBaseCapital(HttpServletRequest request, @RequestBody TBaseCapital tBaseCapital){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseCapitalService.inserttBaseCapital(userVo,tBaseCapital);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     *  删除数据
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value="/delete")
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Map<String,Object> deleteTbaseCapital(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseCapitalService.deletetBaseCapital(userVo,id);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     * 删除数据s
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value="/deletes")
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Map<String,Object> deletesTbaseCapital(HttpServletRequest request,@RequestParam(name = "ids",required=true) String ids){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseCapitalService.deletestBaseCapital(userVo,ids);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }

}
